package handler

import (
	"mpps/tools/config"
	"mpps/tools/middleware"
	_ "mpps/web/docs"
	"mpps/web/routers"

	"github.com/gin-gonic/gin"
	swagFiles "github.com/swaggo/files"
	gs "github.com/swaggo/gin-swagger"
)

func InitRouter() *gin.Engine {
	gin.SetMode(gin.ReleaseMode)
	ginRouter := gin.Default()
	// 处理swagger 跨域问题
	ginRouter.Use(middleware.Cors())
	ginRouter.Use(gin.Recovery())
	ginRouter.Use(middleware.WebAuthMiddleWare())

	// 去掉model模块展示，默认不展开所有组
	ginRouter.GET(
		"/swagger/*any",
		gs.WrapHandler(
			swagFiles.Handler,
			gs.DefaultModelsExpandDepth(-1),
			gs.DocExpansion("none"),
		),
	)

	routers.UserRoutersInit(ginRouter)
	routers.ProgramRoutersInit(ginRouter)

	ginRouter.RunTLS(
		config.G_httpaddr,
		"./conf/ssl/server/server-cert.pem",
		"./conf/ssl/server/server-key.pem",
	)

	return ginRouter
}
